package com.tiantian.quanminxiu.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.jakewharton.rxbinding.view.RxView;
import com.nineoldandroids.animation.Animator;
import com.tiantian.quanminxiu.R;

import cn.pedant.SweetAlert.SweetAlertDialog;
import rx.functions.Action1;

/**
 * Created by admin on 2015/12/21.
 */
public abstract class DialogBase extends Dialog {

    //region 成员变量
    public View relMain;
    protected Context thisContext;
    protected Activity thisActivity;
    protected ApplicationBase thisApplication;
    protected boolean isFirst;
    //endregion

    protected abstract void InitView(Activity activity);

    //region 构造方法 DialogBase(Activity activity,int themeResId)
    protected DialogBase(Activity activity, int themeResId) {
        super(activity, themeResId);
    }
    //endregion

    //region 初始化操作 Init(Activity activity)
    public void Init(Activity activity) {

        thisActivity = activity;
        thisContext = activity.getBaseContext();
        thisApplication = (ApplicationBase) thisContext.getApplicationContext();

        F_setSoftInPut();

        isFirst = true;
        relMain = findViewById(R.id.relMain);
        RxView.clicks(relMain).map(View -> relMain).subscribe(OC_Finish);
    }
    //endregion

    //region 设置软键盘弹出方式 F_setSoftInPut
    public void F_setSoftInPut(){
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }
    //endregion

    //region 初始化启动动画 onWindowFocusChanged
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus&&isFirst) {
            YoYo.with(Techniques.BounceIn).duration(600).playOn(findViewById(R.id.relMain));
            isFirst = false;
        }
    }
    //endregion

    //region 取消事件 OC_Finish
    public Action1<View> OC_Finish = v -> {
        finish(v);
    };
    //endregion

    //region 退出窗口 finish(View v)
    public void finish(View v) {
        if (v != null) {
            YoYo.with(Techniques.ZoomOut)
                    .duration(400)
                    .withListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            dismiss();
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            dismiss();
                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    })
                    .playOn(v);
        }
    }

    public void finish() {
        finish(relMain);
    }
    //endregion

    //region 按回退键退出 onKeyDown(int keyCode, KeyEvent event)
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //endregion

    protected SweetAlertDialog mSweetDialog = null;

    //region 是否显示SweetAlertDialog  SweetDialog_Show()
    protected void SweetDialog_Show() {
        if (mSweetDialog != null && !mSweetDialog.isShowing()) {
            mSweetDialog.show();
        }
    }
    //endregion

    //region 返回控件自带的样式的SweetAlertDialog SweetDialog_Init(int alertType)
    protected SweetAlertDialog SweetDialog_Init(int alertType) {
        if (mSweetDialog == null || !mSweetDialog.isShowing()) {
            mSweetDialog = new SweetAlertDialog(thisActivity);
        }
        mSweetDialog.setOnDismissListener(null);
        mSweetDialog.setOnCancelListener(null);
        mSweetDialog.setOnKeyListener(null);
        mSweetDialog.setOnShowListener(null);
        mSweetDialog.setCanceledOnTouchOutside(true);
        mSweetDialog.setCancelable(true);
        switch (alertType) {
            case SweetAlertDialog.SUCCESS_TYPE:
            case SweetAlertDialog.ERROR_TYPE:
                mSweetDialog.showCancelButton(false)
                        .setConfirmText("确定")
                        .setConfirmClickListener(null)
                        .changeAlertType(alertType);
                break;
            case SweetAlertDialog.PROGRESS_TYPE:
                mSweetDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
                mSweetDialog.setTitleText("Loading")
                        .showCancelButton(false)
                        .showContentText(false)
                        .changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
                mSweetDialog.setCanceledOnTouchOutside(false);
                break;
            case SweetAlertDialog.WARNING_TYPE:
                mSweetDialog.showCancelButton(true)
                        .setCancelClickListener(null)
                        .setConfirmClickListener(null)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
                break;
            case SweetAlertDialog.NORMAL_TYPE:
                mSweetDialog.showCancelButton(false)
                        .setConfirmText("确定")
                        .setConfirmClickListener(null)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
                break;
        }
        return mSweetDialog;
    }
    //endregion

    //region 返回网络异常SweetAlertDialog SweetDialog_InitNetworkTips(String titleText, String contentText)
    protected SweetAlertDialog SweetDialog_InitNetworkTips(String titleText, String contentText) {
        SweetDialog_Init(SweetAlertDialog.WARNING_TYPE)
                .setTitleText(titleText == null ? "网络错误，请检查网络后重试" : titleText)
                .setContentText(contentText)
                .showContentText(contentText != null)
                .setCancelText("取消返回")
                .setConfirmText("确定重试")
                .setCancelClickListener(sDialog -> sDialog.dismiss())
                .setCancelable(false);
        return mSweetDialog;
    }
    //endregion

    //region 返回SweetAlertDialog SweetDialog_InitNetworkTips(String contentText)
    //调用SweetDialog_InitNetworkTips(String titleText, String contentText)
    protected SweetAlertDialog SweetDialog_InitNetworkTips(String contentText) {
        return SweetDialog_InitNetworkTips(null, contentText);
    }
    //endregion

    //region 返回SweetAlertDialog的函数 SweetDialog_InitNetworkTips()
    //调用SweetDialog_InitNetworkTips(String titleText, String contentText)
    protected SweetAlertDialog SweetDialog_InitNetworkTips() {
        return SweetDialog_InitNetworkTips(null, null);
    }
    //endregion

    //region 加载样式 Loading_Open(boolean mark)
    public void Loading_Open(boolean mark) {
        SweetDialog_Init(SweetAlertDialog.PROGRESS_TYPE)
                .setCancelable(!mark);
        SweetDialog_Show();
    }
    //endregion

    //region 加载样式Loading_Open(),调用Loading_Open(mark)
    public void Loading_Open() {
        Loading_Open(false);
    }
    //endregion

    //region 关闭显示加载的AlertDialog Loading_Close()
    public void Loading_Close() {
        if (mSweetDialog != null && mSweetDialog.isShowing() && mSweetDialog.getAlerType() == SweetAlertDialog.PROGRESS_TYPE) {
            mSweetDialog.dismissWithAnimation();
        }
    }
    //endregion
}
